package com.nirvana.data;

import com.nirvana.config.PluginConfig;

import java.util.*;

public class DependenceData {
    private String fullClassName;
    private int index;
    private int show = 1;

    private List<DependenceMetaData> metaDataList;

    private Map<String, DependenceMetaData> metaDataMap;

    public DependenceData() {}

    public DependenceData(String fullClassName) {
        this.fullClassName = fullClassName;
        metaDataList = new ArrayList<>();
        metaDataMap = new HashMap<>();
    }

    public String getFullClassName() {
        return fullClassName;
    }

    public void setFullClassName(String fullClassName) {
        this.fullClassName = fullClassName;
    }

    public List<DependenceMetaData> getMetaDataList() {
        return metaDataList;
    }

    public void setMetaDataList(List<DependenceMetaData> metaDataList) {
        this.metaDataList = metaDataList;
    }

    public int getShow() {
        return show;
    }

    public void setShow(int show) {
        this.show = show;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String simpleName() {
        return PluginConfig.simpleName(fullClassName);
    }

    public void addNewMetaData(String _resolveMethod, int paramTypeIndex, int show, String tooltip) {
        initMetaDataMap();
        String resolveMethod = _resolveMethod;
        if (show == 0) {
            setShow(0);
            if (metaDataMap.containsKey("auto")) return;
            resolveMethod = "auto";
        }
        if (metaDataMap.containsKey(resolveMethod)) return;
        DependenceMetaData metaData = new DependenceMetaData();
        metaData.setResolveMethod(resolveMethod);
        metaData.setParamTypeIndex(paramTypeIndex);
        metaData.setShow(show);
        metaData.setTooltip(tooltip);
        metaDataList.add(metaData);
        metaDataMap.put(resolveMethod, metaData);
    }

    private void initMetaDataMap() {
        if (metaDataMap == null) {
            metaDataMap = new HashMap<>();
            for (DependenceMetaData metaData : metaDataList) {
                metaDataMap.put(metaData.getResolveMethod(), metaData);
            }
        }
    }

    public int resolveIndex(String resolveMethod) {
        for (int i = 0; i < metaDataList.size(); i++) {
            if (metaDataList.get(i).getResolveMethod().equals(resolveMethod)) return i;
        }
        return 0;
    }

    public boolean canImport() {
        return fullClassName.contains(".");
    }

    public String tooltip(String resolveMethod) {
        initMetaDataMap();
        DependenceMetaData orDefault = metaDataMap.getOrDefault(resolveMethod, null);
        if (orDefault == null) return "";
        return orDefault.getTooltip();
    }

    public int paramTypeIndex(String resolveMethod) {
        initMetaDataMap();
        DependenceMetaData orDefault = metaDataMap.getOrDefault(resolveMethod, null);
        if (orDefault == null) return 0;
        return orDefault.getParamTypeIndex();
    }
}
